import { createRouter, createWebHistory } from 'vue-router';

import home from '../views/home.vue';
import friend from '../views/friend.vue';
import setting from '../views/setting.vue';
import pagex from '../views/pagex.vue';
import design from '../views/design.vue';
import formrender from '../views/formrender.vue';
import formone from '../views/formone.vue';


// 创建路由管理器对象，并配置路由表
const router = createRouter({
	history: createWebHistory(),
  routes: [
    { path: '/', component: home},
    { path: '/friend', component: friend},
    { path: '/setting', component: setting},
    { path: '/design', component: design},
    { path: '/formrender', component: formrender},
    { path: '/formone', component: formone},
    { path: '/pages/:sid', component: pagex,beforeEnter:(to,from)=>{
      console.log('--pages--invoked--',to,'---',from);
      return true;
    }},
  ],
});

//全局前置守卫
router.beforeEach((to,from,next)=>{
  console.log('--router-beforeEache--',to,'--',from);

  // 检查用户是否已登录
  // isAuthenticated
  // 避免无限重定向
  // to.name !== 'Login'
  //return { name: 'Login' }

  return next();
  //return true;
})

// router.beforeResolve(async to => {
//   if (to.meta.requiresCamera) {
//     try {
//       await askForCameraPermission()
//     } catch (error) {
//       if (error instanceof NotAllowedError) {
//         // ... 处理错误，然后取消导航
//         return false
//       } else {
//         // 意料之外的错误，取消导航并把错误传给全局处理器
//         throw error
//       }
//     }
//   }
// })

//全局解析守卫: router.beforeResolve
//全局后置钩子: router.afterEach

// 导出路由
export default router;